package com.nisco.dms.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nisco.dms.entity.SqlColumn;
import com.nisco.dms.entity.SqlTable;
import com.nisco.dms.exception.ServiceException;
import com.nisco.dms.service.SqlColumnService;
import com.nisco.dms.service.SqlTableService;
import com.nisco.dms.util.dwz.AjaxObject;
import com.nisco.dms.util.dwz.Page;

/**
 * 
 * @author Robin
 * 
 */
@Controller
@RequestMapping(value = "sqlcolumn")
public class SqlColumnController {
	@Autowired
	private SqlColumnService sqlColumnService;
	@Autowired
	private SqlTableService sqlTableService;
	private static final String CREATE = "sqlcolumn/sqlcolumnCreate";
	private static final String UPDATE = "sqlcolumn/sqlcolumnUpdate";
	private static final String LIST = "sqlcolumn/sqlcolumnList";

	@RequiresPermissions("SqlColumn:save")
	@RequestMapping(value = "/create/{tabid}", method = RequestMethod.GET)
	public String preCreate(@PathVariable Long tabid,Map<String, Object> map) {
		map.put("tabid", tabid);
		return CREATE;
	}

	@RequiresPermissions("SqlColumn:save")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public @ResponseBody
	String create(SqlColumn sqlColumn, HttpServletRequest request) {
		
		sqlColumnService.save(sqlColumn);

		AjaxObject ajaxObject = new AjaxObject(200, "添加成功！",
				"jbsxBox", "", "", "closeCurrent");
		return ajaxObject.toString();
	}

	@RequiresPermissions("SqlColumn:edit")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String preUpdate(@PathVariable String id, Map<String, Object> map) {
		SqlColumn sqlColumn = sqlColumnService.get(id);

		map.put("sqlColumn", sqlColumn);
		return UPDATE;
	}

	@RequiresPermissions("SqlColumn:edit")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody
	String update(SqlColumn sqlColumn) {
		sqlColumnService.update(sqlColumn);

		AjaxObject ajaxObject = new AjaxObject(200, "修改成功！",
				"jbsxBox", "", "", "closeCurrent");
		return ajaxObject.toString();
	}

	@RequiresPermissions("SqlColumn:delete")
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public @ResponseBody
	String delete(@PathVariable String id) {
		AjaxObject ajaxObject = new AjaxObject("删除成功！");
		try {
			sqlColumnService.delete(id);
		} catch (ServiceException e) {
			ajaxObject.setStatusCode(AjaxObject.STATUS_CODE_FAILURE);
			ajaxObject.setMessage("删除失败：" + e.getMessage());
		}
		// 必须设置callBackType否则会调制主页面
		ajaxObject.setCallbackType("");
		return ajaxObject.toString();
	}


	@RequiresPermissions("SqlColumn:view")
	@RequestMapping(value = "/list/{tabid}", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String list(@PathVariable String tabid,Page page, String keywords, Map<String, Object> map) {
		List<SqlColumn> sqlColumns = null;

		if (StringUtils.isNotBlank(keywords)) {
			sqlColumns = sqlColumnService.find(keywords, page);
		} else {
			SqlTable sqlTable = sqlTableService.findOne(tabid);
//			sqlColumns = sqlColumnService.findAll(page);
			sqlColumns = sqlColumnService.findBySqlTable(sqlTable, page);
		}

		map.put("page", page);
		map.put("sqlColumns", sqlColumns);
		map.put("keywords", keywords);
		map.put("tabid", tabid);
		return LIST;
	}
	
}
